package com.bsj.sipgateway.task;

import com.bsj.power.common.def.entity.gb28181.Device;
import com.bsj.power.common.def.entity.job.SysJob;
import com.bsj.power.common.mapper.DeviceMapper;
import com.bsj.power.common.mapper.SysJobMapper;
import com.bsj.sipgateway.sip.transmit.cmd.impl.SIPCommander;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import java.text.ParseException;

/**
 * @author yinhao
 * @version 1.0
 * @description 定时任务调度方法
 * @date 2024/1/24
 */
@Service("tcTask")
@Slf4j
public class TcTask {

    @Resource
    private SysJobMapper sysJobMapper;

    @Autowired
    private SIPCommander sipCommander;

    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 摄像头复位
     */
    public void videoDeviceReset(Integer jobId) {
        log.info("摄像头复位任务执行，当前任务id：{}", jobId);

        SysJob sysJob = sysJobMapper.selectById(jobId);
        if (sysJob == null) {
            log.debug("找不到该任务，jobId=", jobId);
            return;
        }
        String deviceId = sysJob.getDeviceId();
        if (deviceId == null || deviceId.length() == 0) {
            log.debug("该任务没有绑定设备，jobId=", jobId);
        }
        String[] split = deviceId.split(";");
        for (String aString : split) {
            String[] split1 = aString.split(",");
            Long did = Long.parseLong(split1[0]);
            Device device = deviceMapper.selectById(did);

            try {
                sipCommander.frontEndCmd(device.getDeviceId(), 1, 130, 0, 1, 0);
            } catch (SipException e) {
                throw new RuntimeException(e);
            } catch (InvalidArgumentException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }


    }
}
